/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Customer;
import model.Gnome;
import model.GnomeType;



@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Stateful
public class GnomeController 
{
    @PersistenceContext(unitName = "ProjectPU")
    private EntityManager em;
    /**
     * 
     * @return The inventory of gnomes to the gnomeManager
     */
    public ArrayList<Gnome> getInventory () {
        Query q = em.createQuery("SELECT g FROM Gnome g WHERE g.isForSale = :forSale");
        q.setParameter("forSale", 1);
        
        List<Gnome> list = (List<Gnome>) q.getResultList();
        ArrayList<Gnome> inventory = new ArrayList<Gnome>();
        for (Gnome g : list)
            inventory.add(g);
        
        return inventory;
    }
    /**
     * Used to return gnome types to the gnomeManager
     * @return the list of gnome types 
     */
    public ArrayList<GnomeType> getGnomeTypes () {
        Query q = em.createQuery("SELECT gt FROM GnomeType gt");
         
        List<GnomeType> list = (List<GnomeType>) q.getResultList();
        ArrayList<GnomeType> types = new ArrayList<GnomeType>();
        for (GnomeType g : list)
            types.add(g);
        
        return types;
    }
    /**
     * 
     * @param type      Gnome Type
     * @param price     Gnome Price
     * @return          A new gnomeType to the gnomeManager
     */
    public GnomeType createGnomeType (String type,String price) {
        GnomeType gt = new GnomeType (type,Integer.parseInt(price));
        em.persist(gt);
        return gt;
    }
    /**
     * 
     * @param type      Gnome Type
     * @return A new gnome with a certain type to the gnomeManager
     */
    public Gnome createGnomeWithType (GnomeType type) {
        Gnome g = new Gnome(type);
        em.persist(g);
        return g;
    }
    /**
     * Deletes a gnome and returns an updated inventory list
     * @param gnome     Gnome reference
     * @return          the new list that doesn't contain the selected Gnome 
     */
    public ArrayList<Gnome> deleteGnome (Gnome gnome) 
    {
        if(gnome.isIsForSale() == 1) {
            Query q = em.createQuery("DELETE FROM Gnome g WHERE g.id = :id");
            q.setParameter("id", gnome.getId());
            q.executeUpdate();
        }
    
        return getInventory();
         
    }
    /**
     * Calculates the total cost for the Gnome Manager
     * @param gnomes    A list of Gnomes
     * @return          The total cost for given gnomes
     */
    public int getTotalCost (ArrayList<Gnome> gnomes) {
        int sum = 0;
        for (Gnome g : gnomes) sum += g.getType().getPrice();
        return sum;
    }
    /**
     * Calculates the total cost and subtracts the sum from the Customer balance
     * as well as adding the purchased gnomes to the customers collection of gnomes
     * @param gnomes    The shopping cart with gnomes
     * @param name      The logged in user name
     * @param pwd       The logged in password
     * @return 
     */
    public boolean payForCart (ArrayList<Gnome> gnomes,String name,String pwd) 
    {
        int cost = getTotalCost(gnomes);
        Query q = em.createQuery("SELECT c FROM Customer c WHERE c.name LIKE :name AND c.password LIKE :password");
        
        q.setParameter("name",name);
        q.setParameter("password", pwd);
        
        for (Gnome g : gnomes) {
            Gnome gnome = em.find(Gnome.class, g.getId());
            gnome.setIsForSale(0);
        }
        Customer c = (Customer) q.getSingleResult();
        c.getGnomes().addAll(gnomes);
        c.setBalance(c.getBalance()-cost);
        
        em.flush();
        
        return true;
    }
    /**
     * Returns the customers gnomes to the gnomeManager
     * @param name      User Name
     * @param password  Password
     * @return the customers gnome collection
     */
    public ArrayList<Gnome> getCustomersGnomes (String name, String password) 
    {
        Query q = em.createQuery("SELECT c FROM Customer c WHERE c.name LIKE :name AND c.password LIKE :password");
        
        q.setParameter("name", name);
        q.setParameter("password",password);
        
        Customer c = (Customer) q.getSingleResult();
        ArrayList<Gnome> gnomes = new ArrayList<Gnome>();
        gnomes.addAll(c.getGnomes());
        return gnomes;
    }  
}
